// map.js
import qqMap from "../../../utils/qqmap"
Page({
  data: {
    repirId: '',
    latitude: 40.060539,
    longitude: 116.328634,
    dialogVisible: false, //控制弹窗显示与隐藏
    statusMsg: {
      0: '已取消',
      1: '受理中',
      2: '上门中',
      3: '已完成'
    },
    statusType: {
      0: 'cancel',
      1: 'info',
      2: 'success',
      3: 'complete'
    },
    markers: [
      {
        id: 1,
        latitude: 40.086757,
        longitude: 116.328634,
        width: 24,
        height: 30,
      },
      {
        id: 2,
        latitude: 40.08346500000002,
        longitude: 116.33293800000011,
        iconPath: '/static/images/marker.png',
        width: 40,
        height: 40,
      },
    ]
  },
  onLoad({ id }) {
    this.setData({
      repirId: id
    })
    this.getRepairDetail(id)
    this.createPolyLine()
  },
  //获取报修详情
  async getRepairDetail(id) {
    const res = await wx.http.get(`/repair/${id}`)
    // console.log(res, 555555);
    this.setData({
      ...res.data
    })
  },
  //点击取消报修
  onCancel() {
    this.setData({
      dialogVisible: true
    })
  },
  //取消报修
  async onConfirm() {
    await wx.http.put(`/cancel/repaire/${this.data.repirId}`)
    //取消成功提示用户
    wx.utils.toast('取消报修成功')
    //回退
    wx.navigateBack()
  },
  editInfo() {
    wx.navigateTo({
      url: '/repair_pkg/pages/form/index?id=' + this.data.repirId
    })
  },
  //路线规划
  createPolyLine() {
    //调用距离计算接口
    qqMap.direction({
      mode: 'bicycling',
      //from参数不填默认当前地址
      from: '40.060539,116.343847', // 传智播客
      to: '40.086757,116.328634', // （示例）云趣园1区
      success: (res) => {
        let ret = res
        let coords = ret.result.routes[0].polyline, p1 = [];
        //坐标解压（返回的点串坐标，通过前向差分进行压缩）
        let kr = 1000000;
        for (let i = 2; i < coords.length; i++) {
          coords[i] = Number(coords[i - 2]) + Number(coords[i]) / kr
        }
        //将解压后的坐标放入点串数组pl中
        for (let i = 0; i < coords.length; i += 2) {
          p1.push({ latitude: coords[i], longitude: coords[i + 1] })
        }
        //设置polyline属性，将路线显示出来,将解压坐标第一个数据作为起点
        this.setData({
          polyline: [
            {
              points: p1,
              color: '#FF0000DD',
              width: 4,
            },
          ],
        })
      },
      fail: function (error) {
        console.error(error)
      },
    });
  }
})
